package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.joda.time.Duration;

import com.conversationboard.cache.ThreadCache;
import com.conversationboard.cache.UserCache;
import com.conversationboard.database.PerformanceRecord;
import com.conversationboard.formatter.DateFormatter;
import com.conversationboard.model.User;
import com.conversationboard.model.databaseinfo.DatabaseThread;
import com.conversationboard.site.Site;

@WebServlet(name = "PerformanceStatsControllerServlet", urlPatterns = "/Admin/PerformanceStatsControllerServlet")
public class PerformanceStatsControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 190122414987637377L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			Map<String, PerformanceRecord> databaseConnectionInfo = Site.getDatabaseConnectionInfo();
			List<PerformanceRecord> performanceRecords = new LinkedList<PerformanceRecord>();
			long totalCount = 0;
			long totalTime = 0;

			User user = User.get(request.getUserPrincipal());

			for (String key : databaseConnectionInfo.keySet()) {

				PerformanceRecord record = databaseConnectionInfo.get(key);
				totalCount += record.getCount();
				totalTime += record.getTotalTime();

				performanceRecords.add(record);
			}

			Collections.sort(performanceRecords);

			Map<String, String> status = Site.getDatabaseStatus();
			List<DatabaseThread> databaseThreads = Site.getDatabaseThreadList();

			long userCacheEvictions = UserCache.getEvictionCount();
			long threadCacheEvictions = ThreadCache.getCacheEvictions();

			DateTime serverStartTime = new DateTime(Site.getStartupTime());
			String serverUpTime = DateFormatter.prettyPrintTime(serverStartTime.toDate(), user, true, true);

			DateTime now = new DateTime();

			Duration duration = new Duration(serverStartTime, now);
			long secondsSinceStartup = duration.getMillis() / 1000;
			long minutesSinceStartup = duration.getMillis() / 60000;
			long hoursSinceStartup = duration.getMillis() / 3600000;

			request.setAttribute("pageHitCount", Site.getPageHitCount());
			request.setAttribute("showAllCount", Site.getShowAllMessagesHitCount());
			request.setAttribute("serverUpTime", serverUpTime);
			request.setAttribute("secondsSinceStartup", secondsSinceStartup);
			request.setAttribute("minutesSinceStartup", minutesSinceStartup);
			request.setAttribute("hoursSinceStartup", hoursSinceStartup);
			request.setAttribute("userCacheEvictions", userCacheEvictions);
			request.setAttribute("threadCacheEvictions", threadCacheEvictions);

			request.setAttribute("user", user);
			request.setAttribute("databaseThreads", databaseThreads);
			request.setAttribute("status", status);
			request.setAttribute("performanceRecords", performanceRecords);
			request.setAttribute("totalCount", totalCount);
			request.setAttribute("totalTime", totalTime);

			request.setAttribute("maxMemory", Runtime.getRuntime().maxMemory());
			request.setAttribute("totalMemory", Runtime.getRuntime().totalMemory());
			request.setAttribute("freeMemory", Runtime.getRuntime().freeMemory());

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/Admin/performancestats.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

}
